//
//  JSViewController03.swift
//  RXSwiftTest
//
//  Created by ZhangBo on 2019/6/19.
//  Copyright © 2019 ZhangBo. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class JSViewController03: BaseViewController {
    let disposeBag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

}

extension JSViewController03 {
    fileprivate func test01() {
        let observable = Observable<Int>.just(5)
    }
    fileprivate func test02() {
        let observable1 = Observable.of("A", "B", "C")
        let observable2 = Observable.from(["A", "B", "C"])

    }
    fileprivate func test03() {
        let observable1 = Observable<Int>.empty()
        let observable2 = Observable<Int>.never()

    }
    fileprivate func test04() {
        enum MyError: Error {
            case A
            case B
        }
        
        let observable = Observable<Int>.error(MyError.A)
    }
    
    
    fileprivate func test05() {
        //使用range()
        let observable1 = Observable.range(start: 1, count: 5)
        
        //使用of()
        let observable2 = Observable.of(1, 2, 3 ,4 ,5)
        
        
        let observable3 = Observable.repeatElement(1)
    }
    

    fileprivate func test06() {
        //使用generate()方法
        let observable1 = Observable.generate(
            initialState: 0,
            condition: { $0 <= 10 },
            iterate: { $0 + 2 }
        )

        //使用of()方法
        let observable2 = Observable.of(0, 2, 4, 6, 8, 10)
    }


    fileprivate func test07() {
        //这个block有一个回调参数observer就是订阅这个Observable对象的订阅者
        //当一个订阅者订阅这个Observable对象的时候，就会将订阅者作为参数传入这个block来执行一些内容
        let observable = Observable<String>.create { observer in
            //对订阅者发出了.next事件，且携带了一个数据"hangge.com"
            observer.onNext("hangge.com")
            //对订阅者发出了.completed事件
            observer.onCompleted()
            //因为一个订阅行为会有一个Disposable类型的返回值，所以在结尾一定要returen一个Disposable
            return Disposables.create()
        }

        //订阅测试
        observable.subscribe {
            print($0)
        }.disposed(by: disposeBag)
    }

    fileprivate func test08() {
        //用于标记是奇数、还是偶数
        var isOdd = true

        //使用deferred()方法延迟Observable序列的初始化，通过传入的block来实现Observable序列的初始化并且返回。
        let factory: Observable<Int> = Observable.deferred {

            //让每次执行这个block时候都会让奇、偶数进行交替
            isOdd = !isOdd

            //根据isOdd参数，决定创建并返回的是奇数Observable、还是偶数Observable
            if isOdd {
                return Observable.of(1, 3, 5, 7)
            } else {
                return Observable.of(2, 4, 6, 8)
            }
        }

        //第1次订阅测试
        factory.subscribe { event in
            print("\(isOdd)", event)
        }.disposed(by: disposeBag)

        //第2次订阅测试
        factory.subscribe { event in
            print("\(isOdd)", event)
        }.disposed(by: disposeBag)
    }

    fileprivate func test09() {
        let observable = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
        observable.subscribe { event in
            print(event)
        }.disposed(by: disposeBag)
    }

    fileprivate func test10() {
        //5秒种后发出唯一的一个元素0
        let observable1 = Observable<Int>.timer(5, scheduler: MainScheduler.instance)
        observable1.subscribe { event in
            print(event)
        }.disposed(by: disposeBag)

        //延时5秒种后，每隔1秒钟发出一个元素
        let observable2 = Observable<Int>.timer(5, period: 1, scheduler: MainScheduler.instance)
        observable2.subscribe { event in
            print(event)
        }.disposed(by: disposeBag)
    }
}
